﻿Imports SQLtoASPX.Globals
Imports SQLtoASPX.Support
Imports System.IO
Imports System.Text
Imports System.Data.SqlClient


Public Class DDTabPage
    Inherits System.Windows.Forms.TabPage

    Dim VersionNumber As String = ""

    Public DatabaseName As String = ""
    Public dt As DataTable = Nothing
    Public gTableName As String = ""
    Public gSchemaName As String = ""
    Public ServerName As String = ""
    Dim ConnectionString As String = ""

    Public DatabaseServer As DatabaseServer = Nothing

    Private mTreeNode As TreeNode
    Public Property TreeNode() As TreeNode
        Get
            Return mTreeNode
        End Get
        Set(ByVal value As TreeNode)
            mTreeNode = value
        End Set
    End Property

    Public Sub New()

        'Me.Controls.Add(New DataGridView)
        InitializeComponent()
    End Sub

    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents cboSource1TableName As System.Windows.Forms.ComboBox
    Friend WithEvents cboSource1KeyColumn As System.Windows.Forms.ComboBox
    Friend WithEvents cboSource1DisplayColumn As System.Windows.Forms.ComboBox
    Friend WithEvents cboDestinationSourceTable1KeyColumnName As System.Windows.Forms.ComboBox
    Friend WithEvents cboDestinationTableName As System.Windows.Forms.ComboBox
    Friend WithEvents cboDestinationSourceTable2KeyColumnName As System.Windows.Forms.ComboBox
    Friend WithEvents cboSource2DisplayColumn As System.Windows.Forms.ComboBox
    Friend WithEvents cboSource2KeyColumn As System.Windows.Forms.ComboBox
    Friend WithEvents cboSource2TableName As System.Windows.Forms.ComboBox
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents Label9 As System.Windows.Forms.Label
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    Friend WithEvents Label10 As System.Windows.Forms.Label
    Friend WithEvents Label11 As System.Windows.Forms.Label
    Friend WithEvents Label13 As System.Windows.Forms.Label
    Friend WithEvents Label12 As System.Windows.Forms.Label
    Friend WithEvents Label14 As System.Windows.Forms.Label
    Friend WithEvents cmdGenerate As System.Windows.Forms.Button

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.cboDestinationSourceTable1KeyColumnName = New System.Windows.Forms.ComboBox
        Me.cboDestinationSourceTable2KeyColumnName = New System.Windows.Forms.ComboBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label5 = New System.Windows.Forms.Label
        Me.Label6 = New System.Windows.Forms.Label
        Me.cboSource1TableName = New System.Windows.Forms.ComboBox
        Me.cboSource1KeyColumn = New System.Windows.Forms.ComboBox
        Me.cboSource1DisplayColumn = New System.Windows.Forms.ComboBox
        Me.cboDestinationTableName = New System.Windows.Forms.ComboBox
        Me.cboSource2DisplayColumn = New System.Windows.Forms.ComboBox
        Me.cboSource2KeyColumn = New System.Windows.Forms.ComboBox
        Me.cboSource2TableName = New System.Windows.Forms.ComboBox
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.Label9 = New System.Windows.Forms.Label
        Me.Label11 = New System.Windows.Forms.Label
        Me.Label13 = New System.Windows.Forms.Label
        Me.GroupBox3 = New System.Windows.Forms.GroupBox
        Me.Label14 = New System.Windows.Forms.Label
        Me.Label10 = New System.Windows.Forms.Label
        Me.Label12 = New System.Windows.Forms.Label
        Me.cmdGenerate = New System.Windows.Forms.Button
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.GroupBox3.SuspendLayout()
        Me.SuspendLayout()
        '
        'cboDestinationSourceTable1KeyColumnName
        '
        Me.cboDestinationSourceTable1KeyColumnName.BackColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
        Me.cboDestinationSourceTable1KeyColumnName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboDestinationSourceTable1KeyColumnName.FormattingEnabled = True
        Me.cboDestinationSourceTable1KeyColumnName.Location = New System.Drawing.Point(153, 40)
        Me.cboDestinationSourceTable1KeyColumnName.Name = "cboDestinationSourceTable1KeyColumnName"
        Me.cboDestinationSourceTable1KeyColumnName.Size = New System.Drawing.Size(294, 21)
        Me.cboDestinationSourceTable1KeyColumnName.TabIndex = 11
        '
        'cboDestinationSourceTable2KeyColumnName
        '
        Me.cboDestinationSourceTable2KeyColumnName.BackColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer))
        Me.cboDestinationSourceTable2KeyColumnName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboDestinationSourceTable2KeyColumnName.FormattingEnabled = True
        Me.cboDestinationSourceTable2KeyColumnName.Location = New System.Drawing.Point(153, 67)
        Me.cboDestinationSourceTable2KeyColumnName.Name = "cboDestinationSourceTable2KeyColumnName"
        Me.cboDestinationSourceTable2KeyColumnName.Size = New System.Drawing.Size(294, 21)
        Me.cboDestinationSourceTable2KeyColumnName.TabIndex = 13
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(23, 28)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(65, 13)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Table Name"
        '
        'Label5
        '
        Me.Label5.AutoSize = True
        Me.Label5.Location = New System.Drawing.Point(25, 55)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(63, 13)
        Me.Label5.TabIndex = 4
        Me.Label5.Text = "Key Column"
        '
        'Label6
        '
        Me.Label6.AutoSize = True
        Me.Label6.Location = New System.Drawing.Point(9, 88)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(79, 13)
        Me.Label6.TabIndex = 5
        Me.Label6.Text = "Display Column"
        '
        'cboSource1TableName
        '
        Me.cboSource1TableName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboSource1TableName.FormattingEnabled = True
        Me.cboSource1TableName.Location = New System.Drawing.Point(94, 25)
        Me.cboSource1TableName.Name = "cboSource1TableName"
        Me.cboSource1TableName.Size = New System.Drawing.Size(263, 21)
        Me.cboSource1TableName.TabIndex = 8
        '
        'cboSource1KeyColumn
        '
        Me.cboSource1KeyColumn.BackColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
        Me.cboSource1KeyColumn.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboSource1KeyColumn.FormattingEnabled = True
        Me.cboSource1KeyColumn.Location = New System.Drawing.Point(94, 55)
        Me.cboSource1KeyColumn.Name = "cboSource1KeyColumn"
        Me.cboSource1KeyColumn.Size = New System.Drawing.Size(263, 21)
        Me.cboSource1KeyColumn.TabIndex = 9
        '
        'cboSource1DisplayColumn
        '
        Me.cboSource1DisplayColumn.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboSource1DisplayColumn.FormattingEnabled = True
        Me.cboSource1DisplayColumn.Location = New System.Drawing.Point(94, 85)
        Me.cboSource1DisplayColumn.Name = "cboSource1DisplayColumn"
        Me.cboSource1DisplayColumn.Size = New System.Drawing.Size(263, 21)
        Me.cboSource1DisplayColumn.TabIndex = 10
        '
        'cboDestinationTableName
        '
        Me.cboDestinationTableName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboDestinationTableName.FormattingEnabled = True
        Me.cboDestinationTableName.Location = New System.Drawing.Point(153, 13)
        Me.cboDestinationTableName.Name = "cboDestinationTableName"
        Me.cboDestinationTableName.Size = New System.Drawing.Size(294, 21)
        Me.cboDestinationTableName.TabIndex = 12
        '
        'cboSource2DisplayColumn
        '
        Me.cboSource2DisplayColumn.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboSource2DisplayColumn.FormattingEnabled = True
        Me.cboSource2DisplayColumn.Location = New System.Drawing.Point(82, 90)
        Me.cboSource2DisplayColumn.Name = "cboSource2DisplayColumn"
        Me.cboSource2DisplayColumn.Size = New System.Drawing.Size(275, 21)
        Me.cboSource2DisplayColumn.TabIndex = 14
        '
        'cboSource2KeyColumn
        '
        Me.cboSource2KeyColumn.BackColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer))
        Me.cboSource2KeyColumn.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboSource2KeyColumn.FormattingEnabled = True
        Me.cboSource2KeyColumn.Location = New System.Drawing.Point(82, 60)
        Me.cboSource2KeyColumn.Name = "cboSource2KeyColumn"
        Me.cboSource2KeyColumn.Size = New System.Drawing.Size(275, 21)
        Me.cboSource2KeyColumn.TabIndex = 15
        '
        'cboSource2TableName
        '
        Me.cboSource2TableName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboSource2TableName.FormattingEnabled = True
        Me.cboSource2TableName.Location = New System.Drawing.Point(82, 30)
        Me.cboSource2TableName.Name = "cboSource2TableName"
        Me.cboSource2TableName.Size = New System.Drawing.Size(275, 21)
        Me.cboSource2TableName.TabIndex = 16
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.Label1)
        Me.GroupBox1.Controls.Add(Me.cboSource1TableName)
        Me.GroupBox1.Controls.Add(Me.Label5)
        Me.GroupBox1.Controls.Add(Me.Label6)
        Me.GroupBox1.Controls.Add(Me.cboSource1KeyColumn)
        Me.GroupBox1.Controls.Add(Me.cboSource1DisplayColumn)
        Me.GroupBox1.Location = New System.Drawing.Point(12, 12)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(363, 123)
        Me.GroupBox1.TabIndex = 1
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Source Table 1"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.Add(Me.Label9)
        Me.GroupBox2.Controls.Add(Me.cboSource2TableName)
        Me.GroupBox2.Controls.Add(Me.Label11)
        Me.GroupBox2.Controls.Add(Me.Label13)
        Me.GroupBox2.Controls.Add(Me.cboSource2KeyColumn)
        Me.GroupBox2.Controls.Add(Me.cboSource2DisplayColumn)
        Me.GroupBox2.Location = New System.Drawing.Point(381, 12)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(363, 123)
        Me.GroupBox2.TabIndex = 2
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "Source Table 2"
        '
        'Label9
        '
        Me.Label9.AutoSize = True
        Me.Label9.Location = New System.Drawing.Point(11, 33)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(65, 13)
        Me.Label9.TabIndex = 0
        Me.Label9.Text = "Table Name"
        '
        'Label11
        '
        Me.Label11.AutoSize = True
        Me.Label11.Location = New System.Drawing.Point(13, 63)
        Me.Label11.Name = "Label11"
        Me.Label11.Size = New System.Drawing.Size(63, 13)
        Me.Label11.TabIndex = 4
        Me.Label11.Text = "Key Column"
        '
        'Label13
        '
        Me.Label13.AutoSize = True
        Me.Label13.Location = New System.Drawing.Point(-3, 93)
        Me.Label13.Name = "Label13"
        Me.Label13.Size = New System.Drawing.Size(79, 13)
        Me.Label13.TabIndex = 5
        Me.Label13.Text = "Display Column"
        '
        'GroupBox3
        '
        Me.GroupBox3.Controls.Add(Me.Label14)
        Me.GroupBox3.Controls.Add(Me.Label10)
        Me.GroupBox3.Controls.Add(Me.Label12)
        Me.GroupBox3.Controls.Add(Me.cboDestinationTableName)
        Me.GroupBox3.Controls.Add(Me.cboDestinationSourceTable1KeyColumnName)
        Me.GroupBox3.Controls.Add(Me.cboDestinationSourceTable2KeyColumnName)
        Me.GroupBox3.Location = New System.Drawing.Point(159, 154)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(453, 103)
        Me.GroupBox3.TabIndex = 3
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "Destination Table"
        '
        'Label14
        '
        Me.Label14.AutoSize = True
        Me.Label14.Location = New System.Drawing.Point(8, 67)
        Me.Label14.Name = "Label14"
        Me.Label14.Size = New System.Drawing.Size(139, 13)
        Me.Label14.TabIndex = 14
        Me.Label14.Text = "Source Table 2 Key Column"
        '
        'Label10
        '
        Me.Label10.AutoSize = True
        Me.Label10.Location = New System.Drawing.Point(6, 16)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(65, 13)
        Me.Label10.TabIndex = 0
        Me.Label10.Text = "Table Name"
        '
        'Label12
        '
        Me.Label12.AutoSize = True
        Me.Label12.Location = New System.Drawing.Point(8, 40)
        Me.Label12.Name = "Label12"
        Me.Label12.Size = New System.Drawing.Size(139, 13)
        Me.Label12.TabIndex = 4
        Me.Label12.Text = "Source Table 1 Key Column"
        '
        'cmdGenerate
        '
        Me.cmdGenerate.Location = New System.Drawing.Point(240, 263)
        Me.cmdGenerate.Name = "cmdGenerate"
        Me.cmdGenerate.Size = New System.Drawing.Size(292, 23)
        Me.cmdGenerate.TabIndex = 4
        Me.cmdGenerate.Text = "Generate"
        Me.cmdGenerate.UseVisualStyleBackColor = True
        '
        'Form2
        '
        'Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        'Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(796, 614)
        Me.Controls.Add(Me.cmdGenerate)
        Me.Controls.Add(Me.GroupBox3)
        Me.Controls.Add(Me.GroupBox2)
        Me.Controls.Add(Me.GroupBox1)
        Me.Name = "DDTabPage"
        Me.Text = "DDTabPage"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox1.PerformLayout()
        Me.GroupBox2.ResumeLayout(False)
        Me.GroupBox2.PerformLayout()
        Me.GroupBox3.ResumeLayout(False)
        Me.GroupBox3.PerformLayout()
        Me.ResumeLayout(False)

    End Sub

    Public Sub LoadGrid(ByVal e As TreeNode)
        TreeNode = e

        Dim CurrentNode As TreeNode = e
        ' Dim CurrentTable As SQLtoASPX.Table = CurrentNode.Parent.Tag

        dt = GetDataTable()
        ConnectionString = GetConnectionStringFromTreeNode(CurrentNode)
        DatabaseServer = GetDatabaseServerFromTreeNode(CurrentNode)
        Dim Connection As New SqlConnection(ConnectionString)
        Connection.Open()
        'Dim DB As SQLtoASPX.Database = CurrentNode.Parent.Tag
        DatabaseName = CurrentNode.Parent.Text
        ServerName = CurrentNode.Parent.Parent.Text

        gTableName = CurrentNode.Text
        Dim Keys() As String = Split(gTableName, ".")
        gTableName = Keys(1)
        gSchemaName = Keys(0)
        Dim sql As String = "Select @@VERSION"

        Dim Command As New SqlCommand(sql, Connection)
        Dim Version As String = Command.ExecuteScalar

        Version = Replace(Version, "  ", " ")
        Dim VersionKeys() As String = Split(Version, " ")
        VersionNumber = VersionKeys(3)

        'Check 2005 Version todo.
        Debug.Print("Version Number=" & VersionNumber)
        Select Case VersionNumber
            Case "2000"
                sql = "select name from " & DatabaseName & ".sysobjects where xtype = 'U' and name <> 'sysdiagrams' and name <> 'dtproperties' order by xtype,name desc"
            Case Else
                sql = "select name from " & DatabaseName & ".sys.objects where type = 'U' and name <> 'sysdiagrams' and name <> 'dtproperties' order by type, name desc"
        End Select
        Command = New SqlCommand(sql, Connection)
        Debug.Print(sql)
        Dim Reader As SqlDataReader = Nothing
        Try
            Reader = Command.ExecuteReader
        Catch ex As Exception
            Connection.Close()
            Debug.WriteLine(ex.Message)
            MsgBox(ex.Message)
            Exit Sub
        End Try

        Do While Reader.Read
            cboSource1TableName.Items.Add(Reader("Name"))
            cboDestinationTableName.Items.Add(Reader("Name"))
            cboSource2TableName.Items.Add(Reader("Name"))
        Loop
        Reader.Close()
        Connection.Close()

    End Sub

    Private Function GetDataTable() As DataTable
        Dim dt As New DataTable
        Dim DataColumn As New DataColumn
        DataColumn.ColumnName = "ColumnName"
        DataColumn.DataType = GetType(String)
        dt.Columns.Add(DataColumn)

        DataColumn = New DataColumn
        DataColumn.ColumnName = "DataType"
        DataColumn.DataType = GetType(String)
        dt.Columns.Add(DataColumn)

        DataColumn = New DataColumn
        DataColumn.ColumnName = "ShowInMasterView"
        DataColumn.DataType = GetType(Boolean)
        dt.Columns.Add(DataColumn)

        DataColumn = New DataColumn
        DataColumn.ColumnName = "ShowInDetailView"
        DataColumn.DataType = GetType(Boolean)
        dt.Columns.Add(DataColumn)

        DataColumn = New DataColumn
        DataColumn.ColumnName = "SearchType"
        DataColumn.DataType = GetType(String)
        dt.Columns.Add(DataColumn)

        DataColumn = New DataColumn
        DataColumn.ColumnName = "IsGroupColumn"
        DataColumn.DataType = GetType(Boolean)
        dt.Columns.Add(DataColumn)

        DataColumn = New DataColumn
        DataColumn.ColumnName = "AllowNulls"
        DataColumn.DataType = GetType(Boolean)
        dt.Columns.Add(DataColumn)

        DataColumn = New DataColumn
        DataColumn.ColumnName = "ColumnSize"
        DataColumn.DataType = GetType(String)
        dt.Columns.Add(DataColumn)

        DataColumn = New DataColumn
        DataColumn.ColumnName = "IsIdentity"
        DataColumn.DataType = GetType(Boolean)
        dt.Columns.Add(DataColumn)

        DataColumn = New DataColumn
        DataColumn.ColumnName = "ModifyLookup"
        DataColumn.DataType = GetType(String)
        dt.Columns.Add(DataColumn)

        DataColumn = New DataColumn
        DataColumn.ColumnName = "LinkTableName"
        DataColumn.DataType = GetType(String)
        dt.Columns.Add(DataColumn)

        DataColumn = New DataColumn
        DataColumn.ColumnName = "LinkTableKey"
        DataColumn.DataType = GetType(String)
        dt.Columns.Add(DataColumn)

        DataColumn = New DataColumn
        DataColumn.ColumnName = "LinkTableValue"
        DataColumn.DataType = GetType(String)
        dt.Columns.Add(DataColumn)
        Return dt
    End Function

    Private Sub cboDestinationTableName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboDestinationTableName.SelectedIndexChanged
        Dim Connection As New SqlConnection(ConnectionString)
        Connection.Open()

        Dim sql As String = ""
        Select Case VersionNumber
            Case "2000"
                sql = "select c.name from [" & DatabaseName & "].sysobjects o inner join [" & DatabaseName & "].syscolumns c on o.id = c.id where o.name = '" & cboDestinationTableName.Text & "' order by o.name desc"
            Case Else
                sql = "select c.name from [" & DatabaseName & "].sys.objects o inner join [" & DatabaseName & "].sys.columns c on o.object_id = c.object_id where o.name = '" & cboDestinationTableName.Text & "' order by o.name desc"
        End Select
        Dim Command As New SqlCommand(sql, Connection)

        Dim Reader As SqlDataReader = Command.ExecuteReader
        cboDestinationSourceTable1KeyColumnName.Items.Clear()
        cboDestinationSourceTable2KeyColumnName.Items.Clear()

        Do While Reader.Read

            cboDestinationSourceTable1KeyColumnName.Items.Add(Reader("Name"))
            cboDestinationSourceTable2KeyColumnName.Items.Add(Reader("Name"))
        Loop
        Reader.Close()
        Connection.Close()
    End Sub

    Private Sub cboSource1TableName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboSource1TableName.SelectedIndexChanged
        Dim Connection As New SqlConnection(ConnectionString)
        Connection.Open()

        Dim sql As String = ""
        Select Case VersionNumber
            Case "2000"
                sql = "select c.name from [" & DatabaseName & "].sysobjects o inner join [" & DatabaseName & "].syscolumns c on o.id = c.id where o.name = '" & cboSource1TableName.Text & "' order by o.name desc"
            Case Else
                sql = "select c.name from [" & DatabaseName & "].sys.objects o inner join [" & DatabaseName & "].sys.columns c on o.object_id = c.object_id where o.name = '" & cboSource1TableName.Text & "' order by o.name desc"
        End Select
        Dim Command As New SqlCommand(sql, Connection)

        Dim Reader As SqlDataReader = Command.ExecuteReader

        cboSource1DisplayColumn.Items.Clear()
        cboSource1KeyColumn.Items.Clear()

        Do While Reader.Read

            cboSource1DisplayColumn.Items.Add(Reader("Name"))
            cboSource1KeyColumn.Items.Add(Reader("Name"))
        Loop
        Reader.Close()
        Connection.Close()
    End Sub

    Private Sub cboSource2TableName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboSource2TableName.SelectedIndexChanged
        Dim Connection As New SqlConnection(ConnectionString)
        Connection.Open()

        Dim sql As String = ""
        Select Case VersionNumber
            Case "2000"
                sql = "select c.name from [" & DatabaseName & "].sysobjects o inner join [" & DatabaseName & "].syscolumns c on o.id = c.id where o.name = '" & cboSource2TableName.Text & "' order by o.name desc"
            Case Else
                sql = "select c.name from [" & DatabaseName & "].sys.objects o inner join [" & DatabaseName & "].sys.columns c on o.object_id = c.object_id where o.name = '" & cboSource2TableName.Text & "' order by o.name desc"
        End Select
        Dim Command As New SqlCommand(sql, Connection)

        Dim Reader As SqlDataReader = Command.ExecuteReader

        cboSource2DisplayColumn.Items.Clear()
        cboSource2KeyColumn.Items.Clear()

        Do While Reader.Read

            cboSource2DisplayColumn.Items.Add(Reader("Name"))
            cboSource2KeyColumn.Items.Add(Reader("Name"))
        Loop
        Reader.Close()
        Connection.Close()
    End Sub

    Private Sub cmdGenerate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdGenerate.Click
        If cboDestinationSourceTable1KeyColumnName.Text = "" Then
            Logit("All boxes must be filled in to generate, at " & Now.ToString)
            Exit Sub
        End If

        If cboDestinationSourceTable2KeyColumnName.Text = "" Then
            Logit("All boxes must be filled in to generate, at " & Now.ToString)
            Exit Sub
        End If

        If cboSource1DisplayColumn.Text = "" Then
            Logit("All boxes must be filled in to generate, at " & Now.ToString)
            Exit Sub
        End If

        If cboSource1KeyColumn.Text = "" Then
            Logit("All boxes must be filled in to generate, at " & Now.ToString)
            Exit Sub
        End If

        If cboSource2DisplayColumn.Text = "" Then
            Logit("All boxes must be filled in to generate, at " & Now.ToString)
            Exit Sub
        End If

        If cboSource2KeyColumn.Text = "" Then
            Logit("All boxes must be filled in to generate, at " & Now.ToString)
            Exit Sub
        End If

        Logit("Generation Started at " & Now.ToString)

        Dim pcg As New RawCreateScript(gTableName, dt, MasterForm.cboConnectionStringType.SelectedValue, MasterForm.txtConnectionString.Text, MasterForm.cboGridCodingStyle.SelectedValue, True, MasterForm.cboCodeType.SelectedValue, MasterForm.txtProjectName.Text, MasterForm.cboKeyPassing.SelectedValue, MasterForm.cboCommentLevel.SelectedValue, MasterForm.cboPaging.SelectedValue, MasterForm.chkSorting.Checked, "", gSchemaName)
        Dim ddlm As New DragDropListMasterASPX(pcg)
        ddlm.TableName = cboDestinationTableName.Text

        Dim ASPXSource As String = ddlm.ToString(cboDestinationTableName.Text)

        Dim ddlmvb As New DragDropListMasterVB(pcg)

        Dim ASPXVBSource As String = ddlmvb.ToString(cboSource1TableName.Text, cboSource1KeyColumn.Text, cboSource1DisplayColumn.Text, cboDestinationSourceTable1KeyColumnName.Text, cboDestinationTableName.Text, cboDestinationSourceTable2KeyColumnName.Text, cboSource2TableName.Text, cboSource2DisplayColumn.Text, cboSource2KeyColumn.Text)

        Dim FileName As String = MasterForm.txtPath.Text & "\" & cboDestinationTableName.Text & ".aspx"
        Dim fs As New System.IO.FileStream(FileName, FileMode.Create)
        Dim AE As New ASCIIEncoding()
        Dim out As Byte() = AE.GetBytes(ASPXSource)
        fs.Write(out, 0, out.Length)
        fs.Close()
        Logit("Created: " & FileName)

        FileName = MasterForm.txtPath.Text & "\" & cboDestinationTableName.Text & ".aspx.vb"
        fs = New System.IO.FileStream(FileName, FileMode.Create)
        out = AE.GetBytes(ASPXVBSource)
        fs.Write(out, 0, out.Length)
        fs.Close()
        Logit("Created: " & FileName)

        Logit("Generation Completed at " & Now.ToString)

        MsgBox("Generation Complete")

    End Sub

    Private Sub Logit(ByVal s As String)
        MasterForm.txtOutput.Text = s & vbCrLf & MasterForm.txtOutput.Text
    End Sub
End Class
